package tech.bmatch.meituan.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.bmatch.meituan.model.Dishes;
import tech.bmatch.meituan.model.Merchant;
import tech.bmatch.meituan.model.MerchantSearchParam;
import tech.bmatch.meituan.util.DistanceUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 商家服务类的实现，处理增加商家，查询商家以及增加菜品的功能
 */

public class MerchantServiceImpl extends MerchantFileStoreServiceImpl {
    private Map<String, Merchant> merchants;
    private Map<String, Dishes> dishes;
    private static final Logger logger = LoggerFactory.getLogger(MerchantServiceImpl.class);

    public void add(Merchant merchant) {
        /**
         * 初始化merchants
         */
        if (merchants == null) {
            merchants = new HashMap<String, Merchant>();
        }
        //数据校验
        if (merchant == null) {
            return;
        }
        //查重
        for (Map.Entry<String, Merchant> merchantEntry : merchants.entrySet()) {
            Merchant merchant1 = merchantEntry.getValue();
            if (merchant1.getName().equals(merchant.getName()) || merchant1.getId().equals(merchant.getId())) {
                System.out.println("重复输入商家名称或者商家ID，请重新输入");
                return;
            }

        }
        /**
         * 往map中加数据
         */
        merchants.put(merchant.getId(), merchant);
        List<Merchant> merchantList = merchants.values().stream().collect(Collectors.toList());
        store(merchantList);

    }

    /**
     * 搜索商家
     *
     * @param param
     * @return
     */
    public List<Merchant> search(MerchantSearchParam param) {
        List<Merchant> merchantList = new ArrayList<Merchant>();
        for (Map.Entry<String, Merchant> merchantEntry : merchants.entrySet()) {
            Merchant merchant = merchantEntry.getValue();
            Double distance = DistanceUtil.getDistance(param.getLon(), param.getLat(), merchant.getLon(), merchant.getLat());
            merchant.setDistance(distance);
            merchantList.add(merchantEntry.getValue());
        }
        Stream<Merchant> stream = merchantList.stream();
        if (param.getName() != null && !param.getName().equals("")) {
            stream = stream.filter(m ->
                    m.getName().indexOf(param.getName()) >= 0);
        }
        stream = stream.sorted((m1, m2) ->
                ((Double) m1.getDistance()).intValue() -
                        ((Double) m2.getDistance()).intValue());


        return stream.collect(Collectors.toList());
    }

    @Override
    /**
     * 添加菜品
     */
    public void addDishes(Dishes dish) {
        List<Merchant> merchantList = new ArrayList<Merchant>();
        List<Dishes> dishesList = new ArrayList<Dishes>();
        for (Map.Entry<String, Merchant> merchantEntry : merchants.entrySet()) {
            Merchant merchant = merchantEntry.getValue();
            if (merchant.getId().equals(dish.getMerchantId())) {
                dishesList = merchant.getDishes();
                if (dishesList == null) {
                    dishesList = new ArrayList<Dishes>();
                    dishesList.add(dish);
                    merchant.setDishes(dishesList);
                } else {
                    for (int i = 0; i < dishesList.size(); i++) {
                        if (dishesList.get(i).getName().equals(dish.getName())) {
                            System.out.println("菜品重复");
                            return;
                        }
                    }
                    dishesList.add(dish);
                    merchant.setDishes(dishesList);
                }
            }
            merchantList.add(merchant);
        }
        store(merchantList);
    }


}
